Skip to content

fix: keep WindowAggExec's can_repartition when replacing children - #28022

Open
cvp2004 wants to merge 1 commit into
apache:mainfrom
cvp2004:fix/window-agg-can-repartition
Open

cvp2004 wants to merge 1 commit into
apache:mainfrom
cvp2004:fix/window-agg-can-repartition

Conversation

@cvp2004

@cvp2004 cvp2004 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

With datafusion.execution.target_partitions = 1, a query with two window functions over an input with more than one partition fails to plan with a SanityCheckPlan distribution error, while the same query runs with target_partitions = 4:

SET datafusion.execution.target_partitions = 1;
SELECT count(*) OVER (PARTITION BY a + NULL) AS x,
       count(*) OVER (PARTITION BY a) AS y
FROM (SELECT 1 AS a UNION ALL SELECT 2);

target_partitions is a parallelism setting, so it should not decide whether a valid query plans.

The planner builds WindowAggExec with can_repartition = false when target_partitions == 1, so the window requires a single partition. WindowAggExec::replace_children with ChildrenPropertiesMode::Recompute (used by with_new_children) rebuilt the node with can_repartition = true. After sort enforcement replaced its children, the window required KeyPartitioned, the CoalescePartitionsExec below it was no longer needed, and the sanity check rejected the plan.

What changes are included in this PR?

  • WindowAggExec::replace_children passes self.can_repartition instead of true, as BoundedWindowAggExec::replace_children already does.

What is the testing strategy for this PR?

  • A sqllogictest case in window.slt, in the existing section that runs with target_partitions = 1, using the query from the issue. It fails without the change with the SanityCheckPlan error from the issue and returns the expected two rows with it.
  • Ran locally: cargo fmt, cargo clippy -p datafusion-physical-plan --all-targets --all-features -D warnings, cargo test -p datafusion-physical-plan, and the window.slt sqllogictests.

The fallback in enforce_sorting that rebuilds a window with !partition_by().is_empty() also does not carry over can_repartition. I tried a few other window query shapes with target_partitions = 1 and none of them failed, so this PR leaves it unchanged.

Are there any user-facing changes?

Queries like the one above now plan and run with target_partitions = 1. No API changes.

- window_agg_exec.rs: rebuild with self.can_repartition instead of true,
  as BoundedWindowAggExec does, so a window planned with
  target_partitions = 1 keeps requiring a single partition
- window.slt: add the query from apache#26626 as a regression test
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SanityCheckPlan error for two window functions with target_partitions = 1 (WindowAggExec loses can_repartition when its children are replaced)

1 participant